import time
import re
import collections

"""
这个测试，准备将抓取到的nginx日志信息放到一个嵌套的字典中

"""

# Nginx的日志格式要求统一：
'''log_format  access_log_format  '"$remote_addr" "$remote_user" "[$time_local]" "$request" '
              '"$status" "$body_bytes_sent" "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for" "$http_host" "$request_time" "$upstream_response_time" "$upstream_addr" "$upstream_status"';   '''

# 匹配Nginx日志使用统一的正则：
pattern_01 = r'''"(?P<remote_addr>[^"]+)" "(?P<remote_user>[^"]+)" "(?P<time_local>[^"]+)" "(?P<request>[^"]+)" "(?P<status>[^"]+)" "(?P<body_bytes_sent>[^"]+)" "(?P<http_referer>[^"]+)" "(?P<http_user_agent>[^"]+)" "(?P<http_x_forwarded_for>[^"]+)" "(?P<http_host>[^"]+)" "(?P<request_time>[^"]+)" "(?P<upstream_response_time>[^"]+)" "(?P<upstream_addr>[^"]+)" "(?P<upstream_status>[^"]+)"'''

# 需要分析的日志文件路径：
log_file1 = "D:/k12日志分析测试/k12_center-01.log"
# 定义字符串格式化
formatstring = '%-15s %-10s %-12s %8s %10s %10s %10s %10s %10s %10s %10s'


# 统计Nginx日志中各个IP的访问次数
def ip_count():
    # start = time.time()
    # 创建正则的regex对象
    regex = re.compile(pattern_01)
    # 创建一个嵌套字典
    ips = collections.defaultdict(dict)
    # 读取Nginx日志文件
    with open(log_file1, 'r', encoding='utf-8') as f:
        # 读取每行的日志
        for line in f:
            # 将每一行的日志转为字典
            mo = regex.search(line)
            if mo:
                # 获取日志中IP地址的信息
                ip_key = mo.group("remote_addr")
                # 计算ip地址访问的次数times
                if ips.get(ip_key) is None:
                    ips[ip_key]['times'] = 1
                else:
                    ips.get(ip_key)['times'] = ips.get(ip_key).get('times') + 1
                # 计算ip地址的body_bytes_sent

    print(ips)
    # 当统计完毕，将ips字典中，以value为对象，排序
    # c = sorted(ips.items(), key=itemgetter(1), reverse=True)
    # 打印头信息
    # Total ip:
    # print('Total IP: %s' % (len(ips)))
    # # formatstring = '%-15s %-10s %-12s %8s %10s %10s %10s %10s %10s %10s %10s'
    # print('%-15s %-10s' % ('IP', '访问次数'))
    # print('%-15s %-10s' % ('-' * 15, '-' * 10))
    # for item in c:
    #     print('%-15s %-10s' % (item[0], item[1]))
    # end = time.time()
    # print('Running time: %.2f Seconds' % (end - start))


if __name__ == '__main__':
    ip_count()
